//
//  QSProblemSeportViewController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/19.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSProblemSeportViewController.h"
#import "QSWaitingForProcessedCell.h"
#import "QSSendProblemViewController.h"
#import "QSProblemDetailViewController.h"
#import "QSProblemReprotInterface.h"
#import "QSProblemDetailViewController.h"
#import "QSSelectDateView.h"
@interface QSProblemSeportViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (nonatomic, strong)QSSelectDateView *selectDateView;
/** 提交问题Button*/
@property (nonatomic, strong)UIButton *submitButton;

@property (nonatomic, strong)NSString *month;
@end
static NSString *waitingForProcessedCell = @"WaitingForProcessedCell";
@implementation QSProblemSeportViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"问题上报";
    
    [self configSubView];
    [self startRefreshing];
    
}

- (void)configSubView{
   
    [self addRefreshHeader];
    [self addRefreshFooter];
    self.selectDateView = [[QSSelectDateView alloc]initWithFrame:CGRectMake(10, 10, kScreenWidth - 20, kRealValue(40))];
    self.selectDateView.layer.cornerRadius = 5;
    [self.view addSubview:self.selectDateView];
    WeakSelf(weakSelf);
    self.selectDateView.didSelectDateFinishBlock = ^(NSString * _Nonnull date) {
        weakSelf.month = date;
        [weakSelf tableViewShouldUpdateDataByPageIndex:1];
    };
    self.tableView.frame = CGRectMake(0, 50, kScreenWidth, kScreenHeight - kNavgationBarHeight- kRealValue(50) - kRealValue(78));
    [self.tableView registerNib:[UINib nibWithNibName:@"QSWaitingForProcessedCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:waitingForProcessedCell];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    [self.view addSubview:self.submitButton];
    [_submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kRealValue(24));
        make.right.equalTo(self.view).offset(- kRealValue(24));
        make.bottom.equalTo(self.view).offset(- kRealValue(24));
        make.height.offset(44);
    }];
       
}

- (UIButton *)submitButton{
    if (!_submitButton) {
        _submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _submitButton.layer.cornerRadius = 22;
        _submitButton.backgroundColor = [UIColor colorWithHexString:@"#128BEC"];
        [_submitButton setTitle:@"提交问题" forState:UIControlStateNormal];
        [_submitButton setTitleColor:[UIColor QSColorWhiteFFFFFF] forState:UIControlStateNormal];
        _submitButton.titleLabel.font = [UIFont QSFontOfSize16];
        [_submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _submitButton;
}

- (void)submitAction:(UIButton *)sender{
    QSSendProblemViewController *vc = [QSSendProblemViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)tableViewShouldUpdateDataByPageIndex:(NSInteger)pageIndex{
    if (!_month) {
        NSString *curruntDate = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970] *1000];
        self.month = curruntDate;
    }
    [QSAppKeyWindow showIndeterminateHudWithText:@"加载中"];
    [QSProblemReprotInterface getApiProblemReportListWithPageIndex:pageIndex WithMonth:self.month andBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, QSQSProblemReportListModel * _Nonnull result) {
        [QSAppKeyWindow hideHud];
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            if (pageIndex == 1) {
                [self.dataArray removeAllObjects];
            }
            [self.dataArray addObjectsFromArray:result.list];
            self.tableView.emptyDataSetSource = self;
            self.tableView.emptyDataSetDelegate = self;
            [self.tableView reloadData];
            [self.tableView qs_endRefreshing];
            if (pageIndex >= result.pages) {
//                self.tableView.mj_footer.hidden = YES;
                [self.tableView qs_endRefreshingWithNoMoreData];
            }else{
                self.tableView.mj_footer.hidden = NO;
            }
        }
        
    }];
}

#pragma mark - **************** UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    QSWaitingForProcessedCell *cell = [tableView dequeueReusableCellWithIdentifier:waitingForProcessedCell forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kRealValue(95 + 10);//cell间距
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    QSProblemReportModel *model = self.dataArray[indexPath.row];
    
    QSProblemDetailViewController *vc = [QSProblemDetailViewController new];
    vc.problemId = model.id.copy;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - DZNEmptyDataSetSource
/**
 *  返回标题文字
 */
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"暂时没有数据哦!";
    NSDictionary *attributes = @{ NSFontAttributeName: [UIFont boldSystemFontOfSize:14.0f],
                                     NSForegroundColorAttributeName: [UIColor QSColorBlack666666] };
    return [[NSAttributedString alloc]initWithString:text attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    scrollView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    return [UIImage imageNamed:@"ic_problem_null"];
}


- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (self.tableView) {
        [self tableViewShouldUpdateDataByPageIndex:1];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
